package com.ce.cases.execute.logic.search;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.ce.cases.execute.view.CaseSourceExecuteView;
import com.ce.framework.datamaintain.action.SearchAction;
import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.datamaintain.logic.SQLSearchCondition;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.sys.org.dao.po.SysVOrg;
import com.ce.util.OrgUtil;


/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2014-04-06
* 创建时间：16:08:33
* 功能描述：执行入库情况 [CASE_SOURCE_EXECUTE]查询条件类
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class CaseSourceExecuteSearchCondition  extends SQLSearchCondition {

	private String taxpayerNum;
    private String involvedEnt;
    private String caseNum;
    private String recBeginDate;
    private String recEndDate;
    private String caseFrom;
    private String illegalType;
    private String inspectDept;
    private String collectDept;
    
    private String curOrgId;
    private String curPersonId;
    private String curOrgLevel;
    private String curOrgType;    
    private String curRootOrgId;
    private String curRootOrgLevel;
    private String curPersonPosition;//当前人职位
	@Override
	public void initCondition(SearchAction action, boolean search)
			throws AppException {
		User user=User.getUser();
		SysVOrg curOrg=OrgUtil.getOrg(User.getUserOrgId());
		this.curOrgId=curOrg.getVcId();
		this.curOrgLevel=curOrg.getVcOrgLevel();
		this.curPersonId=User.getUserId();
		this.curOrgType=curOrg.getVcType();
		curRootOrgId=user.getRootOrgId();
		curPersonPosition=user.getVcPosition();
		curRootOrgLevel=user.getRootOrgLevel();
	}

   
	public String getSearchSql() throws AppException {
		
		StringBuffer sql = new StringBuffer("select t1.vc_id,t1.case_number,t1.involved_enterprice,t1.vc_end_type" +
						",work.VC_STATUS"+
				         ",t2.exec_person,t2.exec_steps_name,t2.police_help,t2.amount_tax,t2.amount_fine ")
					.append(",(select f.VC_STATUS from T_WORKFLOW_FLOW f where f.VC_RECORD_ID=VC_RECORD_ID and f.VC_WORK_ID=work.vc_id and f.VC_SEQUENCE=work.vc_Status) as vcStatus_")
		         .append(" from case_source t1,case_source_execute t2 ,T_WORKFLOW_WORK work")
		         .append(" where t1.vc_del$flag=0 and t1.vc_id=work.VC_RECORD_ID and t1.vc_id=t2.case_id(+)");
		
//		if("3".equals(this.curOrgLevel)&&"08".equals(curPersonPosition)){//跨区局科长
//			sql.append(" and (work.VC_HANDLE_ORGIDS like '%"+this.curOrgId+"%' or work.VC_HANDLE_ORGID='"+this.curOrgId+"')");
//		}else if("3".equals(this.curOrgLevel)&&"10".equals(curPersonPosition)){//跨区局科员
//				sql.append(" and (work.VC_HANDLE_PERSONIDS like '%"+this.curPersonId+"%' or work.VC_HANDLE_PERSON_ID='"+this.curPersonId+"')");
//		}
		
		if("3".equals(this.curOrgLevel)){
			sql.append(" and t1.STATE=3 and t1.INSPECTION_DEPT_ID ='"+curRootOrgId+"' ");
			//sql.append(" and (work.VC_STATUS='4' or work.VC_STATUS='-2' or work.VC_STATUS='9')");
			if("08".equals(curPersonPosition)){//跨区局科长
				sql.append(" and (work.VC_HANDLE_ORGIDS like '%"+this.curOrgId+"%' or work.VC_HANDLE_ORGID='"+this.curOrgId+"')");
			}else if("10".equals(curPersonPosition)){//跨区局科员
				sql.append(" and (work.VC_HANDLE_PERSONIDS like '%"+this.curPersonId+"%' or work.VC_HANDLE_PERSON_ID='"+this.curPersonId+"')");
			}else{
				sql.append(" and (work.VC_STATUS='21')");//其它人查看，，查询结果为空。VC_STATUS永远不对等于21  
			}
			
		}else{
			sql.append(" and (work.VC_STATUS='21')");//其它人查看，，查询结果为空。VC_STATUS永远不对等于21  
		}
		
		
		//采用变量绑定的方式提高查询效率
		if(StringUtil.isNotBlank(taxpayerNum)){
			sql.append(" and t1.taxpayer_number like ${taxpayerNum}||'%'");
		}
		if(StringUtil.isNotBlank(involvedEnt)){
			sql.append(" and t1.involved_enterprice like '%'||${involvedEnt}||'%'");
		}
		if(StringUtil.isNotBlank(caseNum)){
			sql.append(" and t1.case_number=${caseNum}");
		}
		sql.append(this.getDateCondition("receive_date", recBeginDate, recEndDate));
		if(StringUtil.isNotBlank(caseFrom)){
			sql.append(" and t1.case_from_num=${caseFrom}");
		}
		if(StringUtil.isNotBlank(illegalType)){
			sql.append(" and t1.illegal_type_num=${illegalType}");
		}
		if(StringUtil.isNotBlank(inspectDept)){
			sql.append(" and t1.inspection_dept_id=${inspectDept}");
		}
		if(StringUtil.isNotBlank(collectDept)){
			sql.append(" and t1.collection_dept_id=${collectDept}");
		}
	
		/*=========自行修改该处代码================
		//实现排序的代码
		sql.append(" "+super.getOrderBy());
		======================================*/
		return sql.toString();
	}
	
	public Object fromSQLQuery(ResultSet rs, int num) {
		CaseSourceExecuteView view = new CaseSourceExecuteView();
		try {
			view.setVcId(rs.getString("VC_ID"));
			view.setCaseNumber(rs.getString("CASE_NUMBER"));
			view.setEnterprice(rs.getString("involved_enterprice"));
			view.setExecPerson(rs.getString("exec_person"));
			view.setExecStepsName(rs.getString("exec_steps_name"));
			view.setPoliceHelp(rs.getInt("police_help"));
			view.setAmountTax(rs.getDouble("amount_tax"));
			view.setAmountFine(rs.getDouble("amount_fine"));
			
			view.setVcEndType(rs.getString("VC_END_TYPE"));
			view.setState(rs.getInt("VC_STATUS"));
			view.setVcStatus_(rs.getString("vcStatus_"));
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return view;
	}
    public ISQLMapper getSQLMapper() {
		return this;
	}
	public String getTaxpayerNum() {
		return taxpayerNum;
	}

	public void setTaxpayerNum(String taxpayerNum) {
		this.taxpayerNum = taxpayerNum;
	}

	public String getInvolvedEnt() {
		return involvedEnt;
	}

	public void setInvolvedEnt(String involvedEnt) {
		this.involvedEnt = involvedEnt;
	}

	public String getCaseNum() {
		return caseNum;
	}

	public void setCaseNum(String caseNum) {
		this.caseNum = caseNum;
	}

	public String getRecBeginDate() {
		return recBeginDate;
	}

	public void setRecBeginDate(String recBeginDate) {
		this.recBeginDate = recBeginDate;
	}

	public String getRecEndDate() {
		return recEndDate;
	}

	public void setRecEndDate(String recEndDate) {
		this.recEndDate = recEndDate;
	}

	public String getCaseFrom() {
		return caseFrom;
	}

	public void setCaseFrom(String caseFrom) {
		this.caseFrom = caseFrom;
	}

	public String getIllegalType() {
		return illegalType;
	}

	public void setIllegalType(String illegalType) {
		this.illegalType = illegalType;
	}

	public String getInspectDept() {
		return inspectDept;
	}

	public void setInspectDept(String inspectDept) {
		this.inspectDept = inspectDept;
	}

	public String getCollectDept() {
		return collectDept;
	}

	public void setCollectDept(String collectDept) {
		this.collectDept = collectDept;
	}

	public String getCurOrgId() {
		return curOrgId;
	}

	public void setCurOrgId(String curOrgId) {
		this.curOrgId = curOrgId;
	}

	public String getCurPersonId() {
		return curPersonId;
	}

	public void setCurPersonId(String curPersonId) {
		this.curPersonId = curPersonId;
	}

	public String getCurOrgLevel() {
		return curOrgLevel;
	}

	public void setCurOrgLevel(String curOrgLevel) {
		this.curOrgLevel = curOrgLevel;
	}
	
	
}
